package com.person.controller;

import com.person.common.R;
import com.person.dto.DishDto;
import com.person.pojo.Dish;
import com.person.pojo.DishFlavor;
import com.person.pojo.PageBean;
import com.person.service.DishFlavorService;
import com.person.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/dish")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private DishFlavorService dishFlavorService;

    /**
     * 菜品分页查询
     */
    @GetMapping("/page")
    public R<PageBean> page(@RequestParam(defaultValue = "1") Integer page,
                            @RequestParam(defaultValue = "10") Integer pageSize,
                            String name){

        PageBean dishes= dishService.page(page,pageSize,name);
        return R.success(dishes);
    }

    /**
     * 删除菜品
     */
    @DeleteMapping
    public R<String> delete(@RequestParam List<Long> ids){
        log.info("要删除的id是{}",ids);
        dishService.delete(ids);
        return R.success("删除成功");
    }

    /**
     * 添加菜品
     */
    @PostMapping
    public R<String> addDish(@RequestBody DishDto dto){
        log.info("添加菜品{}",dto);
        dishService.addDish(dto);

        dishFlavorService.addFlavor(dto);
        return R.success("添加成功");
    }

    /**
     * 修改菜品
     */

    //回显菜品信息
    @GetMapping("/{id}")
    public R<DishDto> echoDish(@PathVariable Long id){
        log.info("修改菜品的id是{}",id);
        DishDto DishDto = dishService.echo(id);
        log.info("修改菜品回显信息：{}",DishDto);
        return R.success(DishDto);
    }
    //修改后保存
    @PutMapping
    public R<String> update( @RequestBody DishDto dishDto){
        dishService.update(dishDto);
        dishFlavorService.update(dishDto);
        return R.success("修改成功");
    }

    /**
     * 套餐添加菜品回显
     */
    @GetMapping("/list")
    public R<List<DishDto>> inquire(  Long categoryId){
        log.info("套餐id：{}",categoryId);
        List<DishDto> dishDtos = dishService.selectDish(categoryId);

        for (DishDto dishDto : dishDtos) {
            dishDto.setFlavors(dishFlavorService.getFlavor(dishDto.getId()));
        }
        return R.success(dishDtos);
    }
    /**
     * 停售
     */
    @PostMapping("/status/0")
    public R<String> stopSell(@RequestParam List<Long> ids){
        dishService.stopStatus(ids);
        return R.success("修改成功");
    }
    /**
     * 起售
     */
    @PostMapping("/status/1")
    public R<String> startSell(@RequestParam List<Long> ids) {
        dishService.startStatus(ids);
        return R.success("修改成功");

    }

}
